home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / coff-sh.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  10KB  |  365 lines

  1. /* BFD back-end for Hitachi Super-H COFF binaries.
  2.    Copyright 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.    Written by Steve Chamberlain, <sac@cygnus.com>.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "obstack.h"
  25. #include "libbfd.h"
  26. #include "bfdlink.h"
  27. #include "coff/sh.h"
  28. #include "coff/internal.h"
  29. #include "libcoff.h"
  30.  
  31. static bfd_reloc_status_type sh_reloc();
  32.  
  33. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
  34.  
  35. static reloc_howto_type r_imm32 =
  36.   {R_SH_IMM32,  0, 2, 32, false, 0, 
  37.      complain_overflow_bitfield, sh_reloc,"r_imm32",    true, 0xffffffff,0xffffffff, false};
  38.  
  39.  
  40. /*#define SELECT_RELOC(x,y) x->howto = (&r_imm32)*/
  41.  
  42.  
  43. #define BADMAG(x) SHBADMAG(x)
  44. #define SH 1            /* Customize coffcode.h */
  45.  
  46. #define __A_MAGIC_SET__
  47.  
  48. /* Code to swap in the reloc */
  49. #if 0
  50. #define SWAP_IN_RELOC_OFFSET   bfd_h_get_32
  51. #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
  52. #endif
  53. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  54.   dst->r_stuff[0] = 'S'; \
  55.   dst->r_stuff[1] = 'C';
  56.  
  57. /* Code to turn a r_type into a howto ptr, uses the above howto table
  58.    */
  59. static long
  60. get_symbol_value (symbol)       
  61.      asymbol *symbol;
  62. {                                             
  63.   long relocation = 0;
  64.  
  65.   if (bfd_is_com_section (symbol->section))
  66.   {
  67.     relocation = 0;                           
  68.   }
  69.   else 
  70.   {                                      
  71.     relocation = symbol->value +
  72.      symbol->section->output_section->vma +
  73.       symbol->section->output_offset;
  74.   }                                           
  75.  
  76.   return(relocation);
  77. }
  78.  
  79. #define RTYPE2HOWTO(x,y) ((x)->howto = &r_imm32)
  80.  
  81. /* Perform any necessaru magic to the addend in a reloc entry */
  82.  
  83. #if 0
  84. #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
  85.  cache_ptr->addend =  ext_reloc.r_offset;
  86.  
  87. #endif
  88.  
  89.  
  90. /* Compute the addend of a reloc.  If the reloc is to a common symbol,
  91.    the object file contains the value of the common symbol.  By the
  92.    time this is called, the linker may be using a different symbol
  93.    from a different object file with a different value.  Therefore, we
  94.    hack wildly to locate the original symbol from this file so that we
  95.    can make the correct adjustment.  This macro sets coffsym to the
  96.    symbol from the original file, and uses it to set the addend value
  97.    correctly.  If this is not a common symbol, the usual addend
  98.    calculation is done, except that an additional tweak is needed for
  99.    PC relative relocs.
  100.    FIXME: This macro refers to symbols and asect; these are from the
  101.    calling function, not the macro arguments.  */
  102.  
  103. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)        \
  104.   {                                \
  105.     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;    \
  106.     if (ptr && bfd_asymbol_bfd (ptr) != abfd)            \
  107.       coffsym = (obj_symbols (abfd)                \
  108.              + (cache_ptr->sym_ptr_ptr - symbols));        \
  109.     else if (ptr)                        \
  110.       coffsym = coff_symbol_from (abfd, ptr);            \
  111.     if (coffsym != (coff_symbol_type *) NULL            \
  112.     && coffsym->native->u.syment.n_scnum == 0)        \
  113.       cache_ptr->addend = - coffsym->native->u.syment.n_value;    \
  114.     else if (ptr && bfd_asymbol_bfd (ptr) == abfd        \
  115.          && ptr->section != (asection *) NULL)        \
  116.       cache_ptr->addend = - (ptr->section->vma + ptr->value);    \
  117.     else                            \
  118.       cache_ptr->addend = 0;                    \
  119.   }
  120.  
  121. #if 0
  122. #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
  123.  reloc_processing(relent, reloc, symbols, abfd, section)
  124. #endif
  125.  
  126. /* this function is in charge of performing all the 29k relocations */
  127.  
  128. static bfd_reloc_status_type
  129. sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
  130.         error_message)
  131.      bfd *abfd;
  132.      arelent *reloc_entry;
  133.      asymbol *symbol_in;
  134.      PTR data;
  135.      asection *input_section;
  136.      bfd *output_bfd;
  137.      char **error_message;
  138. {
  139.   /* the consth relocation comes in two parts, we have to remember
  140.      the state between calls, in these variables */
  141.   unsigned long insn;
  142.   unsigned long sym_value;
  143.   unsigned short r_type;
  144.  
  145.   unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
  146.   bfd_byte  *hit_data =addr + (bfd_byte *)(data);
  147.     
  148.   r_type = reloc_entry->howto->type;
  149.  
  150.   if (output_bfd) {
  151.     /* Partial linking - do nothing */
  152.     reloc_entry->address += input_section->output_offset;
  153.     return bfd_reloc_ok;
  154.   }
  155.  
  156.   if (symbol_in != NULL
  157.       && bfd_is_und_section (symbol_in->section))
  158.     {
  159.       /* Keep the state machine happy in case we're called again */
  160.       return (bfd_reloc_undefined);
  161.     }
  162.  
  163.  
  164.   sym_value = get_symbol_value(symbol_in);
  165.  
  166.   switch (r_type) 
  167.     {
  168.     case R_SH_IMM32:
  169.       /* We ignore the previous contents ! */
  170.       insn = sym_value + reloc_entry->addend;  
  171.       bfd_put_32(abfd, insn, hit_data);
  172.       break;
  173.     default:
  174.       *error_message = "Unrecognized reloc";
  175.       return (bfd_reloc_dangerous);
  176.     }
  177.  
  178.  
  179.   return(bfd_reloc_ok);    
  180. }
  181.  
  182. /* The reloc processing routine for the optimized COFF linker.  */
  183.  
  184. static boolean
  185. coff_sh_relocate_section (output_bfd, info, input_bfd, input_section,
  186.                 contents, relocs, syms, sections)
  187.      bfd *output_bfd;
  188.      struct bfd_link_info *info;
  189.      bfd *input_bfd;
  190.      asection *input_section;
  191.      bfd_byte *contents;
  192.      struct internal_reloc *relocs;
  193.      struct internal_syment *syms;
  194.      asection **sections;
  195. {
  196.   struct internal_reloc *rel;
  197.   struct internal_reloc *relend;
  198.  
  199.   /* If we are performing a relocateable link, we don't need to do a
  200.      thing.  The caller will take care of adjusting the reloc
  201.      addresses and symbol indices.  */
  202.   if (info->relocateable)
  203.     return true;
  204.  
  205.  
  206.   rel = relocs;
  207.   relend = rel + input_section->reloc_count;
  208.   for (; rel < relend; rel++)
  209.     {
  210.       long symndx;
  211.       bfd_byte *loc;
  212.       struct coff_link_hash_entry *h;
  213.       struct internal_syment *sym;
  214.       asection *sec;
  215.       bfd_vma val;
  216.  
  217.       symndx = rel->r_symndx;
  218.       loc = contents + rel->r_vaddr - input_section->vma;
  219.  
  220.       if (symndx == -1)
  221.     h = NULL;
  222.       else
  223.     h = obj_coff_sym_hashes (input_bfd)[symndx];
  224.  
  225.       sym = NULL;
  226.       sec = NULL;
  227.       val = 0;
  228.  
  229.  
  230.       if (h == NULL)
  231.     {
  232.       if (symndx == -1)
  233.         sec = bfd_abs_section_ptr;
  234.       else
  235.         {
  236.           sym = syms + symndx;
  237.           sec = sections[symndx];
  238.           val = (sec->output_section->vma
  239.              + sec->output_offset
  240.              + sym->n_value
  241.              - sec->vma 
  242.              - sec->vma);
  243.         }
  244.     }
  245.       else
  246.     {
  247.       if (h->root.type == bfd_link_hash_defined)
  248.         {
  249.           sec = h->root.u.def.section;
  250.           val = (h->root.u.def.value
  251.              + sec->output_section->vma
  252.              + sec->output_offset);
  253.         }
  254.       else
  255.         {
  256.           if (! ((*info->callbacks->undefined_symbol)
  257.              (info, h->root.root.string, input_bfd, input_section,
  258.               rel->r_vaddr - input_section->vma)))
  259.         return false;
  260.         }
  261.     }
  262.  
  263.       switch (rel->r_type)
  264.     {
  265.     default:
  266.       bfd_set_error (bfd_error_bad_value);
  267.       return false;
  268.  
  269.     case R_SH_IMM32:
  270.       {
  271.         long x = bfd_get_32 (input_bfd, loc);
  272.         x += val;
  273.         bfd_put_32 (input_bfd, x, loc);
  274.  
  275.       }
  276.  
  277.       break;
  278.     }
  279.     }     
  280.  
  281.   return true;
  282. }
  283.  
  284. #if 0
  285. static void 
  286. reloc_processing (relent, reloc, symbols, abfd, section)
  287.      arelent * relent;
  288.      struct internal_reloc *reloc;
  289.      asymbol ** symbols;
  290.      bfd * abfd;
  291.      asection * section;
  292. {
  293.   asymbol *ptr;
  294.   relent->address = reloc->r_vaddr;
  295.   relent->howto = &r_imm32;
  296.  
  297.  
  298.   relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
  299.  
  300.   ptr = *(relent->sym_ptr_ptr);
  301.  
  302.   if (ptr 
  303.       && bfd_asymbol_bfd(ptr) == abfd        
  304.  
  305.       && ((ptr->flags & BSF_OLD_COMMON)== 0))    
  306.     {                        
  307.       relent->addend = 0;
  308.     }                        
  309.   else
  310.     {                    
  311.       relent->addend = 0;            
  312.     }            
  313.   relent->address-= section->vma;
  314.  
  315. }
  316. #endif
  317.  
  318. #define coff_relocate_section coff_sh_relocate_section
  319.  
  320. #include "coffcode.h"
  321.  
  322. const bfd_target shcoff_vec =
  323. {
  324.   "coff-sh",            /* name */
  325.   bfd_target_coff_flavour,
  326.   true,                /* data byte order is big */
  327.   true,                /* header byte order is big */
  328.  
  329.   (HAS_RELOC | EXEC_P |        /* object flags */
  330.    HAS_LINENO | HAS_DEBUG |
  331.    HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
  332.  
  333.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  334.   '_',                /* leading symbol underscore */
  335.   '/',                /* ar_pad_char */
  336.   15,                /* ar_max_namelen */
  337.   2,                /* minimum section alignment */
  338.      bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  339.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  340.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  341.      bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  342.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  343.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  344.  
  345.   {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  346.      bfd_generic_archive_p, _bfd_dummy_target},
  347.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  348.      bfd_false},
  349.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  350.      _bfd_write_archive_contents, bfd_false},
  351.  
  352.      BFD_JUMP_TABLE_GENERIC (coff),
  353.      BFD_JUMP_TABLE_COPY (coff),
  354.      BFD_JUMP_TABLE_CORE (_bfd_nocore),
  355.      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  356.      BFD_JUMP_TABLE_SYMBOLS (coff),
  357.      BFD_JUMP_TABLE_RELOCS (coff),
  358.      BFD_JUMP_TABLE_WRITE (coff),
  359.      BFD_JUMP_TABLE_LINK (coff),
  360.      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  361.  
  362.     COFF_SWAP_TABLE,
  363. };
  364.  
  365.